package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.service.ReportService;
import com.sky.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/admin/report")
@Slf4j
public class ReportController {
    @Autowired
    private ReportService reportService;
    @GetMapping("/turnoverStatistics")
    public Result<TurnoverReportVO> turnoverStatistics(@RequestParam String begin, @RequestParam String end) {
        log.info("查询营业额统计数据：{}~{}",begin,end);
        LocalDate beginDate = LocalDate.parse(begin);
        LocalDate endDate = LocalDate.parse(end);
        LocalDateTime beginDateTime = beginDate.atStartOfDay();
        LocalDateTime endDateTime = endDate.atTime(23,59,59);
        TurnoverReportVO turnoverReportVO = reportService.turnoverStatistics(beginDateTime, endDateTime);
        return Result.success(turnoverReportVO);
    }

    @GetMapping("/userStatistics")
    public Result<UserReportVO> userStatistics(@RequestParam String begin, @RequestParam String end) {
        log.info("查询用户统计数据：{}~{}",begin,end);
        LocalDate beginDate = LocalDate.parse(begin);
        LocalDate endDate = LocalDate.parse(end);
        LocalDateTime beginDateTime = beginDate.atTime(23,59,59);;
        LocalDateTime endDateTime = endDate.atTime(23,59,59);
        UserReportVO userReportVO = reportService.userStatistics(beginDateTime, endDateTime);
        return Result.success(userReportVO);
    }
    @GetMapping("/top10")
    public Result<SalesTop10ReportVO> top10(@RequestParam String begin, @RequestParam String end) {
        log.info("查询销量排名top10数据：{}~{}",begin,end);
        LocalDate beginDate = LocalDate.parse(begin);
        LocalDate endDate = LocalDate.parse(end);
        LocalDateTime beginDateTime = beginDate.atStartOfDay();
        LocalDateTime endDateTime = endDate.atTime(23,59,59);
        SalesTop10ReportVO salesTop10ReportVO = reportService.top10(beginDateTime, endDateTime);
        return Result.success(salesTop10ReportVO);
    }

    @GetMapping("/ordersStatistics")
    public Result<OrderReportVO> ordersStatistics(@RequestParam String begin, @RequestParam String end) {
        log.info("查询订单统计数据：{}~{}",begin,end);
        LocalDate beginDate = LocalDate.parse(begin);
        LocalDate endDate = LocalDate.parse(end);
        LocalDateTime beginDateTime = beginDate.atTime(23,59,59);;
        LocalDateTime endDateTime = endDate.atTime(23,59,59);
        OrderReportVO orderReportVO = reportService.ordersStatistics(beginDateTime, endDateTime);
        return Result.success(orderReportVO);
    }

    @GetMapping("/export")
    public void export(HttpServletResponse response) {
        log.info("导出Excel");
        reportService.export(response);
    }
}
